from django.db import models


# Create your models here.

class UserInfo(models.Model):
    username = models.CharField('用户名', max_length=48, db_index=True)
    email = models.EmailField('邮箱', max_length=48)
    mobile_phone = models.CharField('电话号', max_length=48)
    password = models.CharField('密码', max_length=48)

    def __str__(self):
        return self.username

class PricePolicy(models.Model):
    """ 价格策略 """
    category_choices = (
        (1, "免费版"),
        (2, "收费版"),
        (3, "其他"),
    )
    category = models.SmallIntegerField(verbose_name='收费类型', default=2, choices=category_choices)  # 小整数字段
    title = models.CharField(verbose_name='标题', max_length=32)
    price = models.PositiveIntegerField(verbose_name='价格')  # 正整数字段

    project_num = models.PositiveIntegerField(verbose_name='项目数')
    project_member = models.PositiveIntegerField(verbose_name='项目成员数')
    project_space = models.PositiveIntegerField(verbose_name='单项目空间')
    per_file_size = models.PositiveIntegerField(verbose_name='单文件大小(MB)')

    create_datetime = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)


class Transaction(models.Model):
    """ 订单记录 """
    status_choice = (
        (1, '未支付'),
        (2, '已支付')
    )
    status = models.SmallIntegerField(verbose_name='状态', choices=status_choice)

    order = models.CharField(verbose_name='订单号', max_length=64, unique=True)  # 唯一索引

    user = models.ForeignKey(verbose_name='用户', to='UserInfo')
    price_policy = models.ForeignKey(verbose_name='价格策略', to='PricePolicy')

    count = models.IntegerField(verbose_name='数量（年）', help_text='0表示无限期')

    price = models.IntegerField(verbose_name='实际支付价格')

    start_datetime = models.DateTimeField(verbose_name='开始时间', null=True, blank=True)
    end_datetime = models.DateTimeField(verbose_name='结束时间', null=True, blank=True)

    create_datetime = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)


class Project(models.Model):
    """ 项目表 """
    COLOR_CHOICES = (
        (1, "#56b8eb"),  # 56b8eb
        (2, "#f28033"),  # f28033
        (3, "#ebc656"),  # ebc656
        (4, "#a2d148"),  # a2d148
        (5, "#20BFA4"),  # #20BFA4
        (6, "#7461c2"),  # 7461c2,
        (7, "#20bfa3"),  # 20bfa3,
    )

    name = models.CharField(verbose_name='项目名', max_length=32)
    color = models.SmallIntegerField(verbose_name='颜色', choices=COLOR_CHOICES, default=1)
    desc = models.CharField(verbose_name='项目描述', max_length=255, null=True, blank=True)
    use_space = models.IntegerField(verbose_name='项目已使用空间', default=0)
    star = models.BooleanField(verbose_name='星标', default=False)

    join_count = models.SmallIntegerField(verbose_name='参与人数', default=1)
    creator = models.ForeignKey(verbose_name='创建者', to='UserInfo')
    create_datetime = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)

    bucket_name = models.CharField(verbose_name='cos名称', max_length=128)
    bucket_region = models.CharField(verbose_name='cos位置', max_length=32)

    # 查询：可以省事；
    # 增加、删除、修改：无法完成
    # project_user = models.ManyToManyField(to='UserInfo',through="ProjectUser",through_fields=('project','user'))


class ProjectUser(models.Model):
    """ 项目参与者 """
    project = models.ForeignKey(verbose_name='项目', to='Project')
    user = models.ForeignKey(verbose_name='参与者', to='UserInfo')
    star = models.BooleanField(verbose_name='星标', default=False)

    create_datetime = models.DateTimeField(verbose_name='加入时间', auto_now_add=True)


"""
Wiki 模块表结构
"""


class Wiki(models.Model):
    """ wiki文档表结构 """
    title = models.CharField(verbose_name='标题', max_length=32)
    content = models.TextField(verbose_name='内容')
    project = models.ForeignKey(verbose_name='项目', to='Project')
    deepth = models.PositiveIntegerField(verbose_name='深度', default=1)

    parent = models.ForeignKey(verbose_name='父级别目录', to='self', null=True, blank=True, related_name='children')

    def __str__(self):
        return self.title


"""
file 模块表结构
"""


class FileRepository(models.Model):
    """ 文件模块表结构 """
    project = models.ForeignKey(verbose_name='项目', to='Project')
    file_type_choices = (
        (1, '文件'),
        (2, '文件夹')
    )
    file_type = models.SmallIntegerField(verbose_name='类型', choices=file_type_choices)
    name = models.CharField(verbose_name='文件夹名称', max_length=32, help_text='文件/文件夹名')
    key = models.CharField(verbose_name='文件存储在COS中的key', max_length=128, null=True, blank=True)
    file_size = models.IntegerField(verbose_name='文件大小', null=True, blank=True)
    file_path = models.CharField(verbose_name='文件路径', max_length=255, null=True, blank=True)
    parent = models.ForeignKey(verbose_name='父级目录', to='self', related_name='child', null=True, blank=True)
    update_user = models.ForeignKey(verbose_name='最近更新者', to='UserInfo')
    update_datetime = models.DateTimeField(verbose_name='更新时间', auto_now=True)


"""
工单系统设计
"""


class WorkOrder(models.Model):
    """ 工单表 """
    order_id = models.CharField(verbose_name='工单号', max_length=64)
    order_name = models.CharField(verbose_name='工单名称', max_length=128)
    order_desc = models.TextField(verbose_name='工单描述', null=True, blank=True)

    order_type = models.ForeignKey(verbose_name='工单类型', to='WorkOrderType', related_name='orders')
    order_status = models.ForeignKey(verbose_name='工单状态', to='WorkOrderStatus', related_name='orders')

    update_user = models.ForeignKey(verbose_name='更新者', to='UserInfo', related_name='update_orders', null=True,
                                    blank=True)
    assign_user = models.ForeignKey(verbose_name='指派给', to='UserInfo', related_name='assign_orders', null=True,
                                    blank=True)

    status_link = models.TextField(verbose_name='状态链')
    start_time = models.DateTimeField(verbose_name='开始时间', auto_now_add=True)
    update_time = models.DateTimeField(verbose_name='更新时间', auto_now=True)

    project = models.ForeignKey(verbose_name='项目', to='Project')


class WorkOrderType(models.Model):
    """ 工单类型表 """
    order_type_choices = (
        (1, "维护保养"),
        (2, "维修"),
        (3, "请假"),
        (4, "其他"),
    )
    order_type = models.SmallIntegerField(verbose_name='工单类型', default=1, choices=order_type_choices)

    def __str__(self):
        return self.order_type


class WorkOrderStatus(models.Model):
    """ 工单状态表 """
    order_status_choices = (
        (1, '新建'),
        (2, '处理中'),
        (3, '已解决'),
        (4, '已忽略'),
        (5, '待反馈'),
        (6, '已关闭'),
        (7, '重新打开'),
    )
    order_status = models.SmallIntegerField(verbose_name='工单状态', default=1, choices=order_status_choices)

    def __str__(self):
        return self.order_status


class WorkOrderComment(models.Model):
    """ 工单评论表 """
    content = models.CharField(verbose_name='评论内容' ,max_length=256)
    order = models.ForeignKey(verbose_name='所属工单', to='WorkOrder')
    parent_comment = models.ForeignKey(verbose_name='父级评论', to='self', null=True, blank=True)

    user = models.ForeignKey(verbose_name='评论人', to='UserInfo')
